package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * 员工控制类
 *
 * @author tongbing
 * @date 2022/4/21
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController  {


    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;


    /**
     * 登录
     * @param employee 登录员工
     * @return 带token的员工
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee)  {
        log.info("登录员工employee={}",employee );
        return employeeService.adminEmployeeLogin(employee);
    }

    /**
     * 后管登出
     * @return 登出结果
     */
    @PostMapping("/logout")
    public R<String> loginOut(@RequestHeader("Authorization")String token) {
        log.info("退出登录,删除token={}",token );
        return employeeService.adminEmployeeLoginOut(token);
    }


    /**
     *新增员工
     * @param employee 前端传入的员工信息
     * @return 添加结果
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee) {
        log.info("新增员工信息：{}", employee.toString());
        // 调用存储方法
        return  employeeService.adminEmployeeSave(employee);
    }

    /**
     * 通过id查询
     * @param id 员工id
     * @return 查询到的员工数据
     */
    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable String id){
        log.info("通过员工id查询：id{}", id);
        return  employeeService.adminEmployeeSelectById(id);
    }


    /**
     * 分页查询
     * @param name 模糊查询名称
     * @param page 第几页
     * @param pageSize 每页最大显示数
     * @return 查询出的数据
     */
    @GetMapping("/page")
    public R<Page<Employee>> pageSelect(@RequestParam(value = "name",required = false)String name, @RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        log.info(" 分页查询:page={},pageSize={},关键字name={}", page, pageSize, name);
        return  employeeService.adminEmployeePageSelect(name,page,pageSize);

    }


    /**
     * @param token 存入redis数据库的登录用户的token
     * @param employee 修改的员工信息
     * @return 修改结果
     */
    @PutMapping
    public R<String> updateEmployee (@RequestHeader(value = "Authorization",required = false)String token, @RequestBody Employee employee){
        log.info("修改员工信息：{}", employee.toString());
        return employeeService.adminUpdateEmployee(token,employee);
    }



}
